package com.yy.community.modules.comment.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.yy.community.common.utils.PageUtils;
import com.yy.community.common.utils.Query;
import com.yy.community.modules.article.entity.Article;
import com.yy.community.modules.comment.entity.Comment;
import com.yy.community.modules.comment.mapper.CommentMapper;
import com.yy.community.modules.comment.service.ICommentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 司马缸砸缸了
 * @since 2019-07-12
 */
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements ICommentService {


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String articleId = (String)params.get("articleId");
        String userId = (String)params.get("userId");
        String commentLevel = (String)params.get("commentLevel");

        IPage<Comment> page = this.page(new Query<Comment>(params).getPage(),
            Wrappers.<Comment>lambdaQuery()
                    .eq(StringUtils.isNotBlank(articleId), Comment::getArticleId, articleId)
                    .eq(StringUtils.isNotBlank(userId), Comment::getUserId, userId)
                    .eq(StringUtils.isNotBlank(commentLevel), Comment::getCommentLevel, commentLevel)
                    .orderByDesc(Comment::getCreateTime));

        return new PageUtils(page);
    }

    @Override
    public List<Comment> queryList(Comment comment) {
        List<Comment> list = baseMapper.selectList(Wrappers.<Comment>lambdaQuery()
            .eq(StringUtils.isNotBlank(comment.getArticleId()), Comment::getArticleId, comment.getArticleId())
            .eq(StringUtils.isNotBlank(comment.getUserId()), Comment::getUserId, comment.getUserId())
            .eq(comment.getCommentLevel() != null, Comment::getCommentLevel, comment.getCommentLevel())
            .orderByDesc(Comment::getCreateTime));

        return list;
    }

    @Override
    public void deleteComment(Comment comment) {
        baseMapper.delete(Wrappers.<Comment>lambdaQuery()
                .eq(StringUtils.isNotBlank(comment.getArticleId()), Comment::getArticleId, comment.getArticleId())
                .eq(StringUtils.isNotBlank(comment.getUserId()), Comment::getUserId, comment.getUserId())
        );
    }
}
